package com.iretailer.report.analyze.flow.device;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iretailer.report.analyze.annotation.AnalyzeClass;
import com.iretailer.report.analyze.commonModel.InOutMetric;
import com.iretailer.report.analyze.commonModel.PassengerFlowModel;
import com.iretailer.report.analyze.commonModel.QueueModel;
import com.iretailer.report.analyze.commonModel.ServiceModel;
import com.iretailer.report.analyze.commonUtil.ResponeResultType;
import com.iretailer.report.analyze.flow.Flow;
import com.iretailer.report.util.StringUtil;
import com.iretailer.report.util.TimeUtil;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/5/28 20:58
 * @Description:
 */
@AnalyzeClass
@Component
public class ReportV01 extends Flow {

    @Value("${netty.path.reportv01}")
    private String reportv01;

    @Override
    public String[] analyzePath() {
        if (StringUtil.notEmpty(reportv01)) {
            return reportv01.split(",");
        } else {
            return new String[]{"/reportv01"};
        }
    }

    @Override
    public PassengerFlowModel transformation(String context, Map<String, Object> paramsMap) {
        JSONObject object = JSON.parseObject(context);
        PassengerFlowModel model = new PassengerFlowModel();
        model.setChannelCloseType(ResponeResultType.CFLType.CLOSE_ON_FAILURE);
        model.setDeviceId(object.getString("deviceId"));
        model.setMacAddress(object.getString("macAddress"));
        model.setIpAddress(object.getString("ipAddress"));
        model.setTimezone(object.getString("timezone"));
        model.setDeviceType(object.getString("deviceType"));
        model.setSerialNumber(object.getString("serialNumber"));
        model.setSiteName(object.getString("siteName"));
        model.setSiteId(object.getString("siteId"));

        JSONArray inoutArray = object.getJSONArray("inOutMetrics");
        if (inoutArray != null && inoutArray.size() > 0) {
            List<InOutMetric> inOutMetricList = new ArrayList<>();
            for (int i = 0, size = inoutArray.size(); i < size; i++) {
                JSONObject inoutObject = inoutArray.getJSONObject(i);
                InOutMetric io = new InOutMetric();
                io.setZone(inoutObject.getString("zoneId"));
                io.setEnter(inoutObject.getInteger("in"));
                io.setExit(inoutObject.getInteger("out"));
                String startDateTime = inoutObject.getString("startDateTime");
                String endDateTime = inoutObject.getString("endDateTime");
                LocalDateTime start = TimeUtil.parse_LocalDateTime(startDateTime);
                LocalDateTime end = TimeUtil.parse_LocalDateTime(endDateTime);
                io.setDate(start.toLocalDate());
                io.setStartTime(start.toLocalTime());
                io.setEndTime(end.toLocalTime());
                inOutMetricList.add(io);
            }
            model.setInOutMetrics(inOutMetricList);
        }

        JSONArray serviceArray = object.getJSONArray("serviceModels");

        if (serviceArray != null && serviceArray.size() > 0) {
            List<ServiceModel> serviceModels = new ArrayList<>();
            for (int i = 0, size = serviceArray.size(); i < size; i++) {
                JSONObject serviceObject = serviceArray.getJSONObject(i);
                ServiceModel service = new ServiceModel();
                service.setZone(serviceObject.getString("zoneId"));
                service.setNumberServed(serviceObject.getInteger("numberServed"));
                service.setSecondsOccupied(serviceObject.getInteger("secondsOccupied"));

                String startDateTime = serviceObject.getString("startDateTime");
                String endDateTime = serviceObject.getString("endDateTime");
                LocalDateTime start = TimeUtil.parse_LocalDateTime(startDateTime);
                LocalDateTime end = TimeUtil.parse_LocalDateTime(endDateTime);
                service.setDate(start.toLocalDate());
                service.setStartTime(start.toLocalTime());
                service.setEndTime(end.toLocalTime());
                serviceModels.add(service);
            }
            model.setServiceModels(serviceModels);
        }

        JSONArray queueArray = object.getJSONArray("queueModels");

        if (queueArray != null && queueArray.size() > 0) {
            List<QueueModel> queueModels = new ArrayList<>();
            for (int i = 0, size = queueArray.size(); i < size; i++) {
                JSONObject queueObject = queueArray.getJSONObject(i);
                QueueModel queue = new QueueModel();
                queue.setZone(queueObject.getString("zoneId"));
                queue.setAvgLen(queueObject.getDouble("avgLen"));
                queue.setMinLen(queueObject.getDouble("minLen"));
                queue.setMaxLen(queueObject.getDouble("maxLen"));

                String startDateTime = queueObject.getString("startDateTime");
                String endDateTime = queueObject.getString("endDateTime");
                LocalDateTime start = TimeUtil.parse_LocalDateTime(startDateTime);
                LocalDateTime end = TimeUtil.parse_LocalDateTime(endDateTime);
                queue.setDate(start.toLocalDate());
                queue.setStartTime(start.toLocalTime());
                queue.setEndTime(end.toLocalTime());
                queueModels.add(queue);
            }
            model.setQueueModels(queueModels);
        }
        return model;
    }

}
